#ifndef DHD_BUFFERS_H
#define DHD_BUFFERS_H 1

#include <assert.h>
#include <boost/utility.hpp>

#include "refcount.h"

namespace dhd
{

   // Always construct instances of this class via 'dhd_allocate_buffer()'
   // and use the smart pointers exclusively. Atomic increment/decrement
   // is used on the reference count so it's threadsafe.
   class Buffer : public Refcounted, boost::noncopyable
   {
       Buffer();
       
     protected:
       int    total_bytes;
       int    nused;
       char * pbuff;
      
    
       Buffer( char * buff, int size ) : total_bytes(size), nused(0), pbuff(buff)
       {}
      
       
    public:
       
       char       * data()            { return pbuff;       }
       const char * data() const      { return pbuff;       }
       int          size() const      { return nused;       }
       char         set_size( int n ) { nused = n; assert(nused<=total_bytes); }
       int          max_size() const  { return total_bytes; }
   };
   
   
   
   class StaticBuffer : public Buffer
   {
     public:
       
       StaticBuffer( char * buff, int size ) : Buffer( buff, size )
       {}
       
       virtual void on_zero_refcount()
       {}
   };
   
   
   
   typedef boost::intrusive_ptr<Buffer> PBuffer;
   
   // deletes all cached buffers
   void delete_buffer_cache();
   
   // Returns a Buffer of at least nbytes in size
   PBuffer allocate_buffer( int nbytes );
   
}// end namespace dhd
   
#endif

